
var apiHost = './api.php';
var __apiHost = 'http://192.168.0.33:9005/';//http://bsy.5niu.top
    __apiHost = 'http://bsy.5niu.top/';//http://bsy.5niu.top
var __dev = true;
var __transferIndex  = 1;
var __api = {
	code:  '/api/common/get_code',
	login: '/api/doctor/login/index',
	useri: '/api/doctor/service/get_userinfo',
	service:'/api/doctor/service/list',
	body:  '/api/doctor/ai/get_body',
	status:'/api/doctor/ai/get_disease',
	ask:   '/api/doctor/ai/ask',
	aski:   '/api/doctor/ask/ai',
	reserve:'/api/doctor/service/add',
	deatil:'/api/doctor/service/detail',
	finish:'/api/doctor/service/result',
	//login: /api/doctor/service/list
	article:'/admin/article/article/list?&page=1&limit=15',
}
let s= location.origin;
console.log('origin',origin)
if((origin.indexOf('192.168') > -1) || (origin.indexOf('127.0') > -1)){
	let host = "http://192.168.0.123:9005";
	for (let k in __api) {
		__api[k] = host + __api[k];
	}
} else {
	let host = "http://bsy.5niu.top";
	for (let k in __api) {
		__api[k] = host + __api[k];
	}
}
window.log = function() {
	var al = arguments;
	console.log(...al)
}
window.__copy = function (data) {
	var copy = JSON.parse(JSON.stringify(data));
	return copy;
}
window.__ss = function (k,v) {
	if (k) {
		sessionStorage.setItem(k,v);
	}
}
window.__ls = function (k,v) {
	if (k) {
		localStorage.setItem(k,v);
	}
}
window.__gss = function (k) {
	if (k) return sessionStorage.getItem(k);
	return sessionStorage
}
window.__gls = function (k) {
	if (k) return localStorage.getItem(k);
	return localStorage
}
window.__url = function (k) {
	return apiHost;
}
window.__app = {};
window.__router = {};

if (!window.routes) window.routes = [];

var __widget = [
	{
		name: 'App',
		template: `
		<div id="app" class="wh100 pr ofh trs2" name="main" :style="{'opacity': opacity}">
			<div name="tool-box" >
				<Menu></Menu>
			</div>
			<transition name="slide-fade">
				<router-view class="view-style"></router-view>
			</transition >
	  	</div>`,
		data: function () {
			return {
				opacity: 0,
				setting: false,
				isAdmin: false,
				openManager: false,
			}
		},
		watch:{
			'$route' () {
				// this.openManager = false;
			},
		},
		methods: {
			openSetting(){this.setting = true;},
			getList() {
				let v = this;
				let data = {};
				__post(__url('components')).then(r => {
					let l = r.data;
				})
			},
			doLogin() {
				let v = this;
				let fd = new FormData();
				fd.append('username','ysh');
				fd.append('password','123456');
				let dt = {username:'ysh',password:'123456'};
				__post({url:__api.login,data:dt}).then(r => {
					let l = r.data;
				}).catch(e =>{ console.log(e);})
			},
		},
		created: function () {
			let v = this;
			v.opacity = 1;
		},
	},
]
if (!window.widgets) window.widgets = [];
for (let n in __widget) window.widgets.push(__widget[n]);


//reset_password

function _start() {
	const route = [
	{path: '/',name: 'login',component: page_login,}, 
	{path: '/nav',name: 'nav',component: page_nav,}, 
	{path: '/device',name: 'nav',component: () => import('../page/device_page.js?v='+Date.now()),}, 
	{path: '/service',name: 'service',component: page_service,}, 
	{path: '/user_service',name: 'user_service',component: page_user_service,}, // info_service
	{path: '/continue_service',name: 'continue_service',component: page_continue_service,}, 
	{path: '/info_service',name: 'info_service',component: page_info_service,}, 
	{path: '/do_service',name: 'do_service',component: page_do_service,}, 
	{path: '/current_service',name: 'current_service',component: page_current_service,}, 
	{path: '/ing_service',name: 'ing_service',component: page_ing_service,}, 
	{path: '/user_exam',name: 'user_exam',component: page_user_exam,}, 
	//{path: '/do_select',name: 'do_select',component: page_do_select,}, 
	{path: '/do_select',name: 'do_select_first_q',component: page_do_select_f_q,}, 
	{path: '/do_selecti',name: 'do_selecti',component: page_do_selecti,}, 
	{path: '/sub_confirm',name: 'sub_confirm',component: page_sub_confirm,}, 
	{path: '/query_result',name: 'query_result',component: page_query_result,}, 
	{path: '/reset_password',name: 'reset_password',component: page_reset_password,}, 
	]
	// 3. 注册组件
	for (let i = 0; i < widgets.length; i++) {Vue.component(widgets[i].name, widgets[i]);};
	for (let i = 0; i < route.length; i++) routes.push(route[i])
	__router = new VueRouter({
		routes: routes
	})
	__router.beforeEach((to, from, next) => {
		log('beforeEach to',to)
		next();
	})
	// 4. 创建和挂载根实例。
	__app = new Vue({
		router: __router,
		data: {
			opacity: 0,
		},
		methods: {
		},
		mounted: function () {
		}
	}).$mount('#app_dom')
	log('start... ')
}

window.addEventListener('load',_start,false);
var __id = 1;
function __getid () {
	let s = '' + Date.now() + Math.random() + __id;
	s = s.replace(".", "_");
	__id++;
	return s;
}
function __post (url, data, option) {
	// console.log(url)
	var isFormData = function(val) {
		return(typeof FormData !== 'undefined') && (val instanceof FormData);
	}
	if (typeof url == 'object') {
		option = url;
		url = option.url;
		data = option.data;
	}
	let headers = {
		'content-type': 'application/json',
	}
	let body = JSON.stringify(data);
	let method = 'POST';
	if(isFormData(data)) {
		headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
		body = data;
	}
	if (option.method){
		method = option.method;
		if (method == 'GET' && (typeof data == 'object')) {
			let us = url || '';
			let _h = us.indexOf('?') > -1;
			if (!_h) us = us + '?';
			for (let k in  data) {
				let last = us[us.length -1];
				if (last == '?') {
					us = us + k + '=' + data[k];
				} else {
					us = us + '&' + k + '=' + data[k];
				}
			}
			url = us;
			body = null;
		}
	}
	// console.log(isFormData(data),data,headers)
	if (url.indexOf('http://') == -1) url = __apiHost + url;
	return fetch(url, {
		body: body,
		cache: 'no-cache',
		headers: headers,
		method: method,
		// mode: 'no-cors',
		mode: 'cors',
	//}).then(response => response.json()) // parses response to JSON
	}).then( response => {
		return response.json();
	}).catch ( err => {
		console.log(err);
		return err
	})
}
window.__message = function (context, duration, className) {
	
	if ((typeof duration == 'string')) {
		className = duration;
		duration = 5000;
	} else {
		if (!duration) duration = 5000;
	}
	if (!window.__message_list) window.__message_list = [];
	if ((typeof className != 'string')) className = '';
	let dc = function (label, attr) {
	if (!label || (typeof label != 'string')) label = 'div';
		let el = document.createElement(label);
		if (attr && (typeof attr == 'object')) {
			for (let k in attr) {
				let v = attr[k];
				if ((typeof v[0] == 'string') && (typeof v[0] == 'string')) el.setAttribute(v[0],v[1]);
			}
		}
		return el;
	}
	let bcn = '__message_box';
	let box = document.querySelector('.' + bcn);
	if (!box) {
		box = dc('div',[['name',bcn],['class',bcn + ' t0 fs12 w100 z9']])
		box.style.top = '100px'
		document.body.appendChild(box);
	}
	
	let id  = 'm-' + Date.now();
	let msg = dc('div',[['id',id],['class','msg-item-box pf trs3 t0 l0 r0 a z9']]);
	
	let con = dc('div',[['name',id],['class','msg-item-content pr flec center c666']]);
	let content = dc('div',[['name','msg_text'],['class','text-content pa disi bgf msgshadow br2 pdl15 pdr40 pdtb10 sp1 lh20 bbx trs2 ' + className]]);
	let txt = dc('div',[['name','text'],['class','pr wh100']]);
	let ico = dc('div',[['name','close'],['class','pa t0 r0 w30 h40 hovopa1 opa8 btn sen']]);
	let margin = 30;
	let height = 40;
	let items = document.querySelectorAll('.msg-item-box');
	if (items && items.length) {
		let last = items[items.length - 1];
		let ctx  = last.querySelector('.text-content');
		if (ctx) {
			let atop = parseInt(last.getAttribute('top')) || margin;
			height = (ctx.clientHeight) || height;
			margin = atop + height + 10;
		}
	}
	msg.setAttribute('top',margin)
	msg.style.zIndex = (__transferIndex + 10);
	msg.style.top = '' + (margin - 30) + 'px';
	
	txt.innerHTML = context;
	ico.innerHTML = '<i class="fs10 fa fa-close"></i>';
	content.appendChild(txt)
	content.appendChild(ico)
	con.appendChild(content);
	msg.appendChild(con);
	box.appendChild(msg);
	
	let out = setTimeout(function () {
		msg.style.top = '' + margin + 'px';
		msg.style.opacity = 1;
	},20)
	let updateTop = function () {
		let box = document.querySelector('.__message_box');
		let list = box.querySelectorAll('.msg-item-box');
		if (!list.length) return;
		let marginTop = 30;
		for (let k = 0; k < list.length; k++) {
			let me = list[k];
			if (1) {
				me.style.top = '' + marginTop + 'px';
				me.setAttribute('top',marginTop);
				let ctx  = me.querySelector('.text-content');
				let meHeight = 40;
				if (ctx) { meHeight = (ctx.clientHeight) || meHeight; }
				marginTop = marginTop + meHeight + 10;
			}
		}
	}
	let close = function () {
		let currMargin= parseInt(msg.style.top) || "10px";
		msg.style.top = '' + (currMargin - 20) + 'px';
		msg.style.opacity = 0;
		setTimeout(function () {
			let me = box.querySelector('#' + id);
			if (me) box.removeChild(me);
			updateTop();
			for (let i = 0 ; i < __message_list.length; i++) {
				if (__message_list[i].id == msg.id) __message_list.splice(i,1);
			}
		},200)
	}
	ico.addEventListener('click', close, false);
	out = setTimeout(function () {
		ico.removeEventListener('click', close, false);
		msg.close();
	},duration)
	
	msg.timeout = out;
	msg.close   = close;
	msg.updateTop = updateTop;
	__message_list.push(msg)
	return msg;
}
window.__confirm = function (context,wait) {
	if (!wait || (typeof wait != 'number')) wait = 20000;
	return new Promise(function __cfm(resolve, reject){
		let dc = function (label, attr) {
		if (!label || (typeof label != 'string')) label = 'div';
			let el = document.createElement(label);
			if (attr && (typeof attr == 'object')) {
				for (let k in attr) {
					let v = attr[k];
					if ((typeof v[0] == 'string') && (typeof v[0] == 'string')) el.setAttribute(v[0],v[1]);
				}
			}
			return el;
		}
		let bcn = '__confirm_box';
		let box = document.querySelector('.' + bcn);
		if (!box) {
			box = dc('div',[['name',bcn],['class',bcn + ' t0 fs12 w100']])
			box.style.top = '0px'
			document.body.appendChild(box);
		}
		
		let id  = 'm-' + Date.now();
		let cfm = dc('div',[['id',id],['class','cfm-item-box a pf trs3 t0 l0 r0 b0 flex alc center']]);
		
		let mask = dc('div',[['name',id],['class','cfm-item-content pa wh100 bg03 flex alc center c666']]);
		let content = dc('div',[['name','cfm_text'],['class','text-content pr bgf msgshadow br2 pd015 pdtb10 sp1 lh20 bbx trs2 marb5']]);
		let txt = dc('div',[['name','text'],['class','pr w100 pdr40 pdtb10 txt']]);
		let ico = dc('div',[['name','close'],['class','pa t10 r10 w30 h30 hovopa1 opa8 btn sen bg05']]);
		let okb = dc('div',[['name','ok'],['class','h30 flex alc right mart5']]);
		let btn = dc('div',[['name','btn'],['class','pd05 btn h25 w40 bgcr opa8 hovopa1 trs2 cfff']]);
	
		cfm.style.zIndex = (__transferIndex + 10);
		
		txt.innerHTML = context;
		ico.innerHTML = '<i class="fs10 fa fa-close"></i>';
		btn.innerHTML = '<span >确定</span>';
		content.appendChild(txt)
		content.appendChild(ico)
		content.appendChild(okb)
		okb.appendChild(btn)
		cfm.appendChild(mask);
		cfm.appendChild(content);
		box.appendChild(cfm);

		let close = function () {
			ico.removeEventListener('click', close, false);
			btn.removeEventListener('click', ok, false);
			cfm.style.opacity = 0;
			setTimeout(function () {
				let me = box.querySelector('#' + id);
				if (me) box.removeChild(me);
				reject('a');
			},200)
		}
		let ok = function () {
			ico.removeEventListener('click', close, false);
			btn.removeEventListener('click', ok, false);
			cfm.style.opacity = 0;
			setTimeout(function () {
				let me = box.querySelector('#' + id);
				if (me) box.removeChild(me);
				resolve('a');
			},200)
		}
		ico.addEventListener('click', close, false);
		btn.addEventListener('click', ok, false);
		cfm.close   = close;
		setTimeout(function () {
			cfm.style.opacity = 1;
		},20)
		
		setTimeout(function () {
			let me = box.querySelector('#' + id);
			if (me) box.removeChild(me);
			reject('b');
		},wait)
	})
	return cfm;
}
window.__msg = function (r,data) {
	if (r.count === 1) {
		__message('success','cg');
	} else {
		__message(data.action + ' failed!',10000,'cr');
	}
}
